package com.example.trance.filter;

import com.example.util.common.ThreadMdcUtil;
import org.springframework.stereotype.Component;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;


/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2020/12/7 11:17
 */
@Component
public class TraceFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest,ServletResponse servletResponse,FilterChain filterChain) throws IOException, ServletException {
        ThreadMdcUtil.setTraceId();
        try {
            filterChain.doFilter(servletRequest,servletResponse);
        }finally {
            ThreadMdcUtil.removeTranceId();
        }
    }

    @Override
    public void destroy() {

    }

}
